const log4js = require('log4js');
const util = require('util');
log4js.configure({
    appenders: {
        console: {
            type: 'console',
            layout: { type: 'pattern' }
        },
        // 请求日志
        accessLog: {
            type: 'dateFile',
            filename: './logs/access/access',
            alwaysIncludePattern: true,
            pattern: 'yyyy-MM-dd.log',
            numBackups: 10,
            layout: { type: 'pattern', pattern: '%d{yyyy-MM-dd hh:mm:ss}  %m %n%s' }
        },
        // 一般日志
        infoLog: {
            type: 'dateFile',
            filename: './logs/info/info',
            alwaysIncludePattern: true,
            pattern: 'yyyy-MM-dd.log',
            numBackups: 10,
            layout: { type: 'pattern', pattern: '%d{yyyy-MM-dd hh:mm:ss}  %m %n%s' }
        },
        // 警告日志
        warnLog: {
            type: 'dateFile',
            filename: './logs/warning/warning',
            alwaysIncludePattern: true,
            pattern: 'yyyy-MM-dd.log',
            numBackups: 10,
            layout: { type: 'pattern', pattern: '%d{yyyy-MM-dd hh:mm:ss}  %m %n%s' }
        },
        // 错误日志
        errLog: {
            type: 'dateFile',
            filename: './logs/danger/danger',
            alwaysIncludePattern: true,
            pattern: 'yyyy-MM-dd.log',
            numBackups: 10,
            layout: { type: 'pattern', pattern: '%d{yyyy-MM-dd hh:mm:ss}  %m %n%s' }
        }
    },
    categories: {
        default: {
            appenders: ['infoLog', 'console'],
            level: 'all',
            enableCallStack: true
        },
        info: {
            appenders: ['infoLog', 'console'],
            level: 'info',
            enableCallStack: true
        },
        warn: {
            appenders: ['warnLog', 'console'],
            level: 'warn',
            enableCallStack: true
        },
        err: {
            appenders: ['errLog', 'console'],
            level: 'error',
            enableCallStack: true
        },
        access: {
            appenders: ['accessLog'],
            level: 'info',
        }
    },
    pm2: true,
    disableClustering: true,
})
function inspectContent(content) {
    return util.inspect(content, {
        showHidden: false,
        depth: null,
        colors: false
    })
}
exports.warnLog = function (content) {
    const logger = log4js.getLogger('warn')
    logger.warn(inspectContent(content))
}
exports.errLog = function (content) {
    const logger = log4js.getLogger('err')
    logger.error(inspectContent(content))
}
exports.infoLog = function (content) {
    const logger = log4js.getLogger('info')
    logger.info(inspectContent(content))
}
exports.useAccess = function () {
    return log4js.connectLogger(log4js.getLogger('access'), {
        //自定义输出格式
        level: log4js.levels.INFO,
        // :remote-addr 客户端ip地址 :method 请求方式 :url 请求地址 :http-version http版本 :status 状态码 :response-timems 响应时间 :res[content-length] 响应内容长度 :referrer 来源地址 :user-agent 浏览器信息
        format: '[ip]:remote-addr :method :url HTTP/:http-version [状态]:status :response-timems ":referrer" ":user-agent"'
    })
}